function stOut=...
    modeFinderFmincon(xZero,estimopt,...
    parStru,posStru,model,dataStru,prpar,prss,filterStru,flags,struIn)
%
% function [lpostmin,xmax,exitflag,output,Hx]=...
%    modeFinderFmincon(xZero,estimopt,...
%    parStru,posStru,model,dataStru,prpar,prss,filterStru,flags,struIn)
%
%% Inputs 
%
% xZero             (vector)    Initial guess for the estimated parameter *untransformed*
%
% estimopt          (structure) Options for estimation 
%
% All remaining inputs are the same as in *modePosteriorMin.m*. See that function for details 
% must include 
% struIn.transMat   These is *Not* needed for this function. Left in only for compatibility 
%                   with modeFinderCsminwel. In that other code it is a transformation matrix to go from original parameter to
%                   min 
%
%% Output 
% stOut.LPosterior 
% stOut.mode 
% stOut.exitFlag
% stOut.iterations 
% stOut.Hessian
% stOut.time 
% stOut.info

%% Notes 
% Uses *modePosteriorMin.m* for evaluation of the posterior 
% When called from the Parallel Loop Uses silent csminwel
%
% This is the modeFinder version of *lmjMinFMincon.m*
% =========================================================================
if any(imag(xZero)~=0);error('Initial guess X0 cannot be imaginary');end

%% 1. Truncate bounds 
% ========================================================================
% Define the bounds for the optimization to ensure compatibility with
% INDMAT. Also check suggested starting point is within bounds, else adjust
% BOUNDS should NOT be truncated 
% ========================================================================
offset=1e-12; 
bounds=[prpar.lbnd(posStru.param.est)+offset prpar.ubnd(posStru.param.est)-offset]; 
check1=find(xZero < bounds(:,1));
check2=find(xZero > bounds(:,2)); 
if ~isempty(check1) 
    check1_disc=abs(xZero(check1)-prpar.lbnd(check1)); 
    if max(check1_disc) > 0.01;error('Starting value is below lower bound');end
    xZero(check1)=xZero(check1)+check1_disc+offset; 
end 
if ~isempty(check2) 
    check2_disc=abs(xZero(check2)-prpar.ubnd(check2)); 
    if max(check2_disc) > 0.01;
        dispaj('Positions ',check2(:),' above upper bound')
        error('Starting value is above upper bound')
    end
    xZero(check2)=xZero(check2)-check2_disc-offset;
end 
clear check* offset; 
% ========================================================================
% Begin Optimization  
% ========================================================================

%% 2. Define default output 
stOut.LPosterior=inf;
stOut.mode=nan(length(xZero),1);
stOut.iterations=-1;
stOut.exitFlag=-10;
stOut.tiempo=0;
stOut.Hessian=nan(length(xZero));
stOut.info={'Problems at start'};

%% 3. Define the anonymous function to minimize 
fmin=@(x) modePosteriorMin(x,parStru,posStru,model,dataStru,prpar,prss,filterStru,flags); 

flags.transform=0; 
tic;

%% 4. Begin fmincon 
[stOut.mode,stOut.LPosterior,stOut.exitFlag,stOut.info,~,~,stOut.Hessian]=...
    fmincon(fmin,xZero,[],[],[],[],bounds(:,1),bounds(:,2),[],estimopt);

stOut.time=toc; 
stOut.iterations=stOut.info.iterations;
